/**
* This is about <code>org.shoebox.guacamole.materials.AnimateMaterial2</code>.
* {@link www.hyperfiction.fr}
* @author shoe[box]
*/

package org.shoebox.guacamole.materials {

	import org.shoebox.guacamole.view.GuaMovieClip;
	import org.shoebox.guacamole.view.abstracts.GuaMesh;
	import org.shoebox.utils.logger.Logger;
	import flash.display3D.Context3DVertexBufferFormat;
	import flash.utils.Dictionary;
	
	/**
	* org.shoebox.guacamole.materials.AnimateMaterial2
	* @author shoebox
	*/
	public class AnimateMaterial extends AtlasMaterial{
		
		protected var _dCycleslens				: Dictionary;
		protected var _dCycles					: Dictionary;
		protected var _vCycles 				: Vector.<Vector.<TextureRegion>>;
		protected var _vCyclesName				: Vector.<String>;
		protected var _xDesc					: XML;
		
		// -------o constructor
		
			/**
			* Constructor of the AnimateMaterial2 class
			*
			* @public
			* @return	void
			*/
			public function AnimateMaterial( from : GuaTexture , name : String = null ) : void {
				super( from.tTexture , from.iSourceW , from.iSourceH , false , name );
			}

		// -------o public
			
			/**
			* Parse Sparrow <code>XML</code> datas
			* 
			* @public
			* @param 	x : XML datas			( XML )
			* @return	void
			*/
			final public function parseSparrow( xDesc : XML ) : void {
				_parseSparrow( xDesc );
			}
			
			/**
			* preprod function
			* @public
			* @param 
			* @return
			*/
			override public function preprod( mesh : GuaMesh ) : void {
				
				if( mesh is GuaMovieClip )
					_preprodMovieClip( mesh as GuaMovieClip );
				else
					super.preprod( mesh );
					
			}
			
		// -------o protected
			
			/**
			* 
			*
			* @param 
			* @return
			*/
			final protected function _preprodMovieClip( oMesh : GuaMovieClip ) : void {
				
				//
					if( !oMesh.currentLabel )
						oMesh.currentLabel = _dCycles[ 0 ];
				
				//
					var iCycleId 	: int = _dCycles[ oMesh.currentLabel ];
					var iFrameId 	: int = oMesh.currentFrame;
					var iCycleLen 	: int = _dCycleslens[ oMesh.currentLabel ];
					
				//
					var oCycle : TextureRegion = _vCycles[ iCycleId ][ iFrameId ];
					oMesh.setUvs( oCycle.getCoordsVec( ) , Context3DVertexBufferFormat.FLOAT_2 );
				
				//
					oMesh.currentFrame++;
					if( oMesh.currentFrame >= iCycleLen )
						oMesh.currentFrame = 0;	
						
				//
					super.preprod( oMesh );
			}
			
			/**
			* 
			*
			* @param 
			* @return
			*/
			final protected function _parseSparrow( xDesc : XML ) : void {
				
				//
					_dCycles 		= new Dictionary( true );
					_dCycleslens 	= new Dictionary( true );
					_xDesc			= xDesc;
					
				//
					var a : Array;
					var i : int = 0 , l : int = xDesc.SubTexture.length( ) , x : XML , s : String , sCycleName : String , iCyclesCount : int = 0 , iCycleLen : int;
					
				//
					var vNames : Vector.<String> = new Vector.<String>( );
					for( i ; i < l ; i++ ){
						
						x = xDesc..SubTexture[ i ];
						s = x.@name.toString( );
						
						if( s.indexOf('/') !== -1 ){
							a = s.split('/');
							
							sCycleName = a[ 0 ];
							if( _dCycleslens[ sCycleName ]== undefined  ){
								iCycleLen = 1;
								_dCycles[ sCycleName ] = iCyclesCount;
								iCyclesCount++;
								vNames.push( sCycleName );
							}else
								iCycleLen++;
								
							_dCycleslens[ sCycleName ] = iCycleLen;
						}else{
							addRegion( s , x.@x , x.@y , x.@width , x.@height );
						}
					}
				
				//
					_vCyclesName = new Vector.<String>( iCyclesCount );
					_vCycles = new Vector.<Vector.<TextureRegion>>( iCyclesCount , true );
					i = 0;
					for( i ; i < iCyclesCount ; i++ ){
						
						_vCyclesName[ i ] = sCycleName = vNames[ i ];
						_vCycles[ i ] = _parseCycle( sCycleName );
						
					}
				
			}
			
			/**
			* 
			*
			* @param 
			* @return
			*/
			final protected function _parseCycle( sCycleName : String ) : Vector.<TextureRegion> {
				
				//
					var 	xFrames : XMLList = _xDesc..SubTexture.( String( @name ).match('^'+sCycleName+'/') );
					var		l : int = xFrames.length( ) , i : int = 0 , x : XML;
					var 	vRes : Vector.<TextureRegion> = new Vector.<TextureRegion>( l , true );
					var 	oReg : TextureRegion;
					
				//
					for( i ; i < l ; i++ ){
						
						x = xFrames[ i ];
						
						oReg = new TextureRegion( x.@name );
						oReg.dx = Number( x.@x ) / sourceWidth;
						oReg.dy = Number( x.@y ) / sourceHeight;
						oReg.width = Number( x.@width ) / sourceWidth;
						oReg.height = Number( x.@height ) / sourceHeight;
						vRes[ i ] = oReg;
						
					}
				
				return vRes;
			}
			
			/**
			* 
			*
			* @param 
			* @return
			*/
			final protected function _addCycleFrame( sCode : String , xDesc : XML ) : void {
				
				
				//var sCycleName : String = a[ 0 ];
				
			}
			
		// -------o misc

			public static function trc(...args : *) : void {
				Logger.log(AnimateMaterial, args);
			}
	}
}
